#delimit;
clear all;
set more off;
set logtype text;
capture log close table_birthsum;


** REPLACE FILE PATH WITH PATH TO RELEVANT REPLICATION FILES;
local fileloc = "~/KMS_REPLICATION";

log using `fileloc'/log_files/table_birthsum.txt, replace name(table_birthsum);

**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
** TABLE 3; 
**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;

** Timeframe - 2002 through 2007, last birth in 2006;
use `fileloc'/data/birth_data/daily_births_and_deaths.dta if birth_week >= tw(2002w1), clear;

** Drop fetal deaths;
drop if fetal_death == 1;

** Generate mortality variable for summary purposes;

** Non-missing death week indicates infant mortality observation;
gen died = (death_week ~= .);
sum died;
count if died == 1;

gen age_in_weeks = death_week - birth_week;
** Replace censored data (no death within 52 weeks) with full life span of 52 weeks;
replace age_in_weeks = 52 if age_in_weeks == .;

sort mother_zip;
tempfile births;
save `births';

** Run data preparation file – see do file `all_regression_prep_hazard_collapsed';
** This makes sure we match to only the zip codes used in our main analysis;
do `fileloc'/all_regression_prep_hazard_collapsed.do `fileloc' _all;
** Sample one observation zip code and keep only zip code variable for merging – this selects only zip codes used in our main analysis;
bysort mother_zip: keep if _n == 1;
keep mother_zip;
joinby mother_zip using `births';
gen year = year(dofw(birth_week));


***** VARIABLE LABELS;
label variable died "Infant Death";
label variable male "Male";
label variable black "Black";
label variable asian "Asian";
label variable hisp "Hispanic";
label variable other_race "Other Race";
label variable HS_grad "Mother is HS Grad";
label variable college_grad  "Mother is College Grad";
label variable twins  "Twins";
label variable trip_or_more "Triplets or More";
label variable age19_25 "Mother Age 19 - 25";
label variable age26_30 "Mother Age 26 - 30";
label variable age31_35 "Mother Age 31 - 35";
label variable age36up "Mother Age > 35";
label variable medicaid "Medicaid";
label variable care_first_tri "Care 1st Trimester";
label variable low_weight "Low Birth Weight";
label variable premature "Premature";

estpost tabstat died, statistics(mean sd) listwise columns(statistics) by(year);

esttab using `fileloc'/regs/birthsum1.tex, 
	replace
	main(mean) aux(sd)
	label;

estpost tabstat male black asian hisp other_race  HS_grad college_grad twins trip_or_more age19_25 age26_30 age31_35 age36up medicaid care_first_tri low_weight premature, statistics(mean) listwise columns(statistics);

esttab using `fileloc'/regs/birthsum2.tex, 
	replace
	main(mean) aux(sd)
	label;
	
log close table_birthsum;